package com.hgw.imsever.redis;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableConfigurationProperties(RedisProperty.class)
public class JedisConfig {

    @Autowired
    private RedisProperty redisProperty;

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisProperty.getMaxIdle());
        poolConfig.setMaxWaitMillis(redisProperty.getMaxWaitMills());

        if (StringUtils.isBlank(redisProperty.getPassword())){
            redisProperty.setPassword(null);
        }

        JedisPool jedisPool = new JedisPool(poolConfig, redisProperty.getHost(),
                redisProperty.getPort(), redisProperty.getTimeOut(), redisProperty.getPassword());
        return jedisPool;
    }

}
